package com.lz.es;

/**
 * @description
 *
 * @date 2021-11-25 21:13
 *
 * @author Lizhong
 */

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.elasticsearch.action.admin.indices.create.CreateIndexRequestBuilder;
import org.elasticsearch.action.admin.indices.create.CreateIndexResponse;
import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequestBuilder;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsResponse;
import org.elasticsearch.action.bulk.BulkRequestBuilder;
import org.elasticsearch.action.bulk.BulkResponse;
import org.elasticsearch.action.delete.DeleteResponse;
import org.elasticsearch.action.get.GetResponse;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.action.support.master.AcknowledgedResponse;
import org.elasticsearch.action.update.UpdateRequest;
import org.elasticsearch.action.update.UpdateResponse;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.index.query.QueryBuilder;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.search.SearchHit;
import org.elasticsearch.search.SearchHits;
import org.elasticsearch.transport.client.PreBuiltTransportClient;

import java.io.IOException;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * 访问ES的工具类
 * @see 命令行参考https://github.com/elastic/elasticsearch
 *
 */
public class EsTool {
    /**
     * 查看ES路径:elasticsearch-7.5.1\config\elasticsearch.yml cluster.name
     */
    private static final String CLUSTER_NAME = "docker-cluster";

    private static EsTool esTool;

    private static TransportClient transportClient;

    public static EsTool getInstance() {
        if (null == esTool) {
            esTool = new EsTool();
        }
        return esTool;
    }

    /**
     * 减少频繁获取连接，定义一个变量存放连接
     * @return
     */
    @SuppressWarnings("deprecation")
    public TransportClient getClient() {
        if (null == transportClient) {
            transportClient = getNewClient();
        }
        return transportClient;
    }

    public static void main(String[] args) throws IOException {
        String operate = "addIndicesMapping";
        // String operate = "addDocument";
        if ("addIndicesMapping".equals(operate)) {
            // 添加索引

            String jsonString = "{\"properties\":" +
                    "{\"author\":{\"type\":\"keyword\"}," +
                    "\"title\":{\"type\":\"text\"}," +
                    "\"content\":{\"type\":\"text\"}," +
                    "\"price\":{\"type\":\"integer\"}," +
                    "\"date\":{\"type\":\"date\",\"format\":\"yyyy-MM-dd HH:mm:ss\"}}}";
            // 执行成功后显示:----------Add mapping success----------true
            EsTool instance = getInstance();
            instance.deleteIndexAndMapping("indices_test");
            instance.createIndexAndMapping("indices_test", jsonString);
        } else if ("addOrUpdateDocument".equals(operate)) {
            // bulk success
            getInstance().bulkIndexDocument("indices_test", "_doc", null);
        } else if ("deleteById".equals(operate)) {
            getInstance().deleteById("indices_test", "_doc", "id_003");
        } else if ("batchDeleteByIds".equals(operate)) {
            List<String> ids = new ArrayList<String>();
            ids.add("id_001");
            ids.add("id_002");
            getInstance().batchDeleteByIds("indices_test", "_doc", ids);
        } else if ("updateDocument".equals(operate)) {
            // result is OK   == id指_id
            getInstance().updateDocument("indices_test", "_doc", "TNZDUHEB_rQdj7R3LnO4", null);
        } else if ("updateDocumentPrepare".equals(operate)) {
            // result is UPDATED   == id指_id
            getInstance().updateDocumentPrepare("indices_test", "_doc", "TNZDUHEB_rQdj7R3LnO4", null);
        } else if ("searchByIndex".equals(operate)) {
            // id指_id
            getInstance().searchByIndex("indices_test", "_doc", "TNZDUHEB_rQdj7R3LnO4");
        } else if ("queryAll".equals(operate)) {
            // ..."totalHits":{"value":3,"relation":"EQUAL_TO"},"maxScore":1.0}
            getInstance().queryAll("indices_test");
        } else if ("search".equals(operate)) {
            // 查询全部
            QueryBuilder queryBuilder = QueryBuilders.matchAllQuery();
            getInstance().searchQuery("indices_test", queryBuilder);
            //以下内容仅仅为插叙条件格式
            // Span First
            /*QueryBuilder queryBuilder = QueryBuilders.spanFirstQuery(
                    QueryBuilders.spanTermQuery("title", "title"), 1);*/

            /*QueryBuilder queryBuilder =QueryBuilders.spanNearQuery(QueryBuilders.spanTermQuery("title", "title"),1000)
            .addClause(QueryBuilders.spanTermQuery("title", "title_001"))
            .addClause(QueryBuilders.spanTermQuery("title", "title_002"))
            .addClause(QueryBuilders.spanTermQuery("title", "title_003"));*/
            // ...
        }
    }

    /**
     * 根据不同的条件查询
     * @throws Exception
     */
    public void searchQuery(String index, QueryBuilder queryBuilder) {
        SearchResponse response = getClient().prepareSearch(index).setQuery(queryBuilder).get();
        for (SearchHit searchHit : response.getHits()) {
            System.out.println(searchHit);
        }
    }

    /**
     * 根据索引、类型、id获取记录
     * @param index
     * @param type
     * @param id
     */
    public void searchByIndex(String index, String type, String id) {
        GetResponse response = getClient().prepareGet(index, type, id).execute().actionGet();
        String json = response.getSourceAsString();
        if (null != json) {
            System.out.println(json);
        } else {
            System.out.println("no result");
        }
    }

    /**
     * 修改内容
     * @throws Exception
     */
    public boolean updateDocumentPrepare(String index, String type, String id, XContentBuilder source) {
        XContentBuilder endObject;
        try {
            // 修改后的内容
            endObject = XContentFactory.jsonBuilder().startObject().field("author", "test_prepare_001").endObject();
            UpdateResponse response = getClient().prepareUpdate(index, type, id).setDoc(endObject).get();
            System.out.println("result is " + response.getResult().name());
            return "UPDATED".equals(response.getResult().name());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 修改内容
     * @return
     */
    @SuppressWarnings("deprecation")
    public boolean updateDocument(String index, String type, String id, XContentBuilder source) {
        Date time = new Date();
        // 创建修改请求
        UpdateRequest updateRequest = new UpdateRequest();
        updateRequest.index(index);
        updateRequest.type(type);
        updateRequest.id(id);
        try {
            // 根据实际需要调整方法参数source里的值
            updateRequest.doc(XContentFactory.jsonBuilder().startObject().field("author", "author001").field("title", "title001")
                    .field("content", "content001")
                    .field("date", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(time)).endObject());
            UpdateResponse response = getClient().update(updateRequest).get();
            System.out.println("result is " + response.status().name());
            return "OK".equals(response.status().name());
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ExecutionException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return false;
    }

    /**
     * 根据id批量删除
     * @param index
     * @param type
     * @param ids
     * @return
     */
    public boolean batchDeleteByIds(String index, String type, List<String> ids) {
        if (null == ids || ids.isEmpty()) {
            System.out.println("ids is require");
            return true;
        }
        BulkRequestBuilder builder = getClient().prepareBulk();
        for (String id : ids) {
            builder.add(getClient().prepareDelete(index, type, id).request());
        }
        BulkResponse bulkResponse = builder.get();
        System.out.println(bulkResponse.status());
        if (bulkResponse.hasFailures()) {
            System.out.println("has failed, " + bulkResponse.status().name());
            return false;
        }
        return true;
    }

    /**
     * 根据索引名称、类型和id删除记录
     * @param indexName
     * @param type
     * @param id
     */
    public void deleteById(String indexName, String type, String id) {
        DeleteResponse dResponse = getClient().prepareDelete(indexName, type, id).execute().actionGet();
        if ("OK".equals(dResponse.status().name())) {
            System.out.println("delete id success");
        } else {
            System.out.println("delete id failed : " + dResponse.getResult().toString());
        }
    }

    /**
     * 删除某个索引下所有数据
     * @param indexName
     * @return
     */
    public boolean deleteAllIndex(String indexName) {
        if (null == indexName || "".equals(indexName.trim())) {
            System.out.println("Error: index name is require.");
            return false;
        }
        //如果传人的indexName不存在会出现异常.可以先判断索引是否存在：
        IndicesExistsRequest inExistsRequest = new IndicesExistsRequest(indexName);
        IndicesExistsResponse inExistsResponse = getClient().admin().indices()
                .exists(inExistsRequest).actionGet();
        if (inExistsResponse.isExists()) {
            AcknowledgedResponse response = getClient().admin().indices().prepareDelete(indexName)
                    .execute().actionGet();
            System.out.println("delete index date, result is " + response.isAcknowledged());
            return response.isAcknowledged();
        } else {
            System.out.println("index is not existed");
        }
        return true;
    }

    /**
     * 查询索引下的全部数据
     * @param index
     * @param type
     */
    public void queryAll(String index) {
        // 创建SearchRequest对象,并指定要操作的索引对象


        SearchResponse response = getClient().prepareSearch(index).setQuery(QueryBuilders.matchAllQuery()).setSize(10000).get();

        SearchHits resultHits = response.getHits();
        System.out.println(resultHits.getTotalHits());
        for (SearchHit hit : resultHits.getHits()) {
            System.out.println(hit);
        }
    }

    /**
     * 添加或者修改ES里的数据
     * @param index
     * @param type
     */
    public void bulkIndexDocument(String index, String type, List<JSONObject> data) {
        BulkRequestBuilder bulkRequest = getClient().prepareBulk();
        Date time = new Date();
        for (JSONObject jsonObject : data) {

            bulkRequest.add(getClient().prepareIndex(index, type, jsonObject.get("id").toString())
                    .setSource(jsonObject));
        }
        BulkResponse bulkResponse = bulkRequest.get();
        if (bulkResponse.hasFailures()) {
            // process failures by iterating through each bulk response item
            System.out.println("bulk has failed and token " + bulkResponse.getTook());
        } else {
            System.out.println("bulk success");
        }
    }

    /**
     * 创建索引和mapping
     * @param indiceName
     * @throws Exception
     */
    public boolean createIndexAndMapping(String indiceName, Object json) {
        if (null == indiceName || "".equals(indiceName.trim())) {
            System.out.println("indice is required");
            return false;
        }
        String content = "content";
        CreateIndexRequestBuilder cib = getClient().admin().indices().prepareCreate(indiceName);
        XContentBuilder builderMapping = generateMappingBuilder(json);
        cib.addMapping(content, builderMapping);
        CreateIndexResponse res = cib.execute().actionGet();
        if (res.isAcknowledged()) {
            System.out.println("----------Add mapping success----------" + res.isAcknowledged());
        } else {
            System.out.println("----------Add mapping failed-----------" + res.isAcknowledged());
        }
        return res.isAcknowledged();
    }

    public boolean deleteIndexAndMapping(String indiceName) {
        if (null == indiceName || "".equals(indiceName.trim())) {
            System.out.println("indice is required");
            return false;
        }
        String content = "content";
        DeleteIndexRequestBuilder cib = getClient().admin().indices().prepareDelete(indiceName);
        AcknowledgedResponse res = cib.execute().actionGet();
        if (res.isAcknowledged()) {
            System.out.println("----------delete mapping success----------" + res.isAcknowledged());
        } else {
            System.out.println("----------delete mapping failed-----------" + res.isAcknowledged());
        }
        return res.isAcknowledged();
    }

    /**
     * 根据json动态构造mapping索引对应的XContentBuilder
     * @param objJson
     * @param builder
     * @param isBegin 是否是开始位置
     */
    private XContentBuilder generateMappingBuilder(Object object) {
        XContentBuilder builder = null;
        try {
            builder = XContentFactory.jsonBuilder();
            JSONObject jsonObj = null;
            if (object instanceof String) {
                jsonObj = JSON.parseObject((String) object);
            }
            // json对象
            generateMappingBuilder(jsonObj, builder, true);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("get json builder error");
        }
        return builder;
    }

    /**
     * 根据json对象动态构造mapping索引对应的XContentBuilder
     * @param objJson
     * @param builder
     * @param isBegin 是否是开始位置
     */
    private void generateMappingBuilder(Object objJson, XContentBuilder builder, boolean isBegin) {
        try {
            // #builder构造,需要添加一个开始"{"
            if (isBegin) {
                builder.startObject();
            }
            // json数组
            if (objJson instanceof JSONArray) {
                JSONArray objArray = (JSONArray) objJson;
                for (int i = 0; i < objArray.size(); i++) {
                    generateMappingBuilder(objArray.get(i), builder, false);
                }
            }
            // json对象
            else if (objJson instanceof JSONObject) {
                JSONObject jsonObject = (JSONObject) objJson;
                Iterator<String> it = jsonObject.keySet().iterator();
                while (it.hasNext()) {
                    String key = it.next().toString();
                    Object object = jsonObject.get(key);

                    // builder:key;这里区分object和普通的属性(冒号前认为为对象,冒号后为属性)
                    if (!key.equals("type") && !key.equals("format")) {
                        builder.startObject(key);
                        // System.out.println("==" + key);
                    }

                    // json数组
                    if (object instanceof JSONArray) {
                        JSONArray objArray = (JSONArray) object;
                        generateMappingBuilder(objArray, builder, false);
                    }
                    // json对象
                    else if (object instanceof JSONObject) {
                        generateMappingBuilder((JSONObject) object, builder, false);
                    }
                    // 其他
                    else {
                        builder.field(key, object.toString());
                        // System.out.println("====" + key + "," + object.toString());
                    }
                }
                // #builder构造,需要添加一个结束"}"
                builder.endObject();
                // System.out.println("==");
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("generate mapping builder failed");
        }
    }

    /**
     *
     * @return
     * @throws IOException
     */
    private XContentBuilder generateMapping() throws IOException {
        XContentBuilder builder = XContentFactory.jsonBuilder();
        builder = builder
                // #builer开始"{"
                .startObject()
                .startObject("properties") //设置之定义字段
                .startObject("author")
                .field("type", "keyword") //设置数据类型
                .endObject()
                .startObject("title")
                .field("type", "text")
                .endObject()
                .startObject("content")
                .field("type", "text")
                .endObject()
                .startObject("price")
                .field("type", "integer")
                .endObject()
                .startObject("date")
                .field("type", "date")  //设置Date类型
                .field("format", "yyyy-MM-dd HH:mm:ss") //设置Date的格式
                .endObject()
                .endObject()
                // #builer结束"}"
                .endObject();
        return builder;
    }

    /**
     * 获取访问ES的连接
     * @return
     */
    @SuppressWarnings({"deprecation", "resource"})
    private TransportClient getNewClient() {
        TransportClient client = null;
        try {
            Settings settings = Settings.builder().put("cluster.name", CLUSTER_NAME)
                    // 开启嗅探功能(即自动检测集群内其他的节点和新加入的节点);或者全部用addTransportAddress添加，如下：
                    // .put("client.transport.sniff", true)
                    .build();
            client = new PreBuiltTransportClient(settings)
                    .addTransportAddress(new TransportAddress(InetAddress.getByName("test.com"), 9300));
        } catch (UnknownHostException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            System.out.println("get host error");
        }
        return client;
    }
}